package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.constant.WorkConstant;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjWorkCancel;
import com.zb.mapper.ZjWorkCancelMapper;
import com.zb.service.ZjWorkCancelService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 工单取消表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-11-29
 */
@Service
public class ZjWorkCancelServiceImpl extends ServiceImpl<ZjWorkCancelMapper, ZjWorkCancel> implements ZjWorkCancelService {

    // 保存服务商取消工单申请
    @Override
    public void saveNetworkApplyCancelOrder(AccounLoginInfo loginInfo, Long workId, String remark) {
        ZjWorkCancel cancel = new ZjWorkCancel();
        cancel.setId(IdWorker.getId());
        cancel.setCustAccId(loginInfo.getCustAccId());
        cancel.setWorkId(workId);
        cancel.setStatus(WorkConstant.WORK_CANCEL_STATUS_1);
        cancel.setApplyEmpType(2);
        cancel.setApplyEmpId(loginInfo.getId());
        cancel.setApplyEmp(loginInfo.getName());
        cancel.setApplyRemark(remark);
        cancel.setReviewEmpId(0L);
        cancel.setReviewRemark("");
        this.save(cancel);
    }

    // 根据[工单ID]&[服务商ID]与[服务商人员ID]获取取消工单信息
    @Override
    public ZjWorkCancel getCancelOrderByWorkIdAndNetworkIdWidthNetworkEmpId(Long workId, Long networkId, Long networkEmpId) {
        QueryWrapper<ZjWorkCancel> wrapper = new QueryWrapper<>();
        wrapper.eq("work_id", workId);
        wrapper.between("status", WorkConstant.WORK_CANCEL_STATUS_1, WorkConstant.WORK_CANCEL_STATUS_2);
        if (null != networkId && networkId > 0) {
            wrapper.eq("network_id", networkId);
        }
        if (null != networkEmpId && networkEmpId > 0) {
            wrapper.eq("network_emp_id", networkEmpId);
        }
        return this.getOne(wrapper);
    }

    // 根据工单ID获取待审核的取消工单数据
    @Override
    public ZjWorkCancel getWaitCheckCancelOrder(Long workId) {
        QueryWrapper<ZjWorkCancel> wrapper = new QueryWrapper<>();
        wrapper.select("id", "status", "apply_emp_type", "apply_emp", "apply_remark", "gmt_create")
                .eq("status", WorkConstant.WORK_CANCEL_STATUS_1).eq("work_id", workId);
        return this.getOne(wrapper);
    }

    @Override
    public List<ZjWorkCancel> getWaitCheckCancelOrderByNetworkIdAndWorkIds(Long networkId, List<Long> workIds) {
        if (CollectionUtil.isEmpty(workIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkCancel> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_id", "status", "apply_emp_type", "apply_emp", "apply_remark", "gmt_create")
                .eq("status", WorkConstant.WORK_CANCEL_STATUS_1)
                .eq("network_id", networkId)
                .in("work_id", workIds);
        return this.list(wrapper);
    }
}
